package com.huichenghe.xinlvsh01.expand_activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.huichenghe.xinlvsh01.Adapter.MyViewPagerAdapter;
import com.huichenghe.xinlvsh01.R;
import com.huichenghe.xinlvsh01.fragment.TrendMonthFragment;
import com.huichenghe.xinlvsh01.fragment.TrendWeekFragment;

import java.util.ArrayList;

/**
 * 趋势界面
 * 本界面包含两个fragment
 */
public class TrendActivity extends AppCompatActivity
        implements TrendMonthFragment.OnFragmentInteractionListener,
                   TrendWeekFragment.OnFragmentInteractionListener
{
    private ArrayList<Fragment> fragmentList;   // fragment集合
    private TrendWeekFragment weekFragment;     // 周趋势fragment
    private TrendMonthFragment monthFragment;   // 月趋势fragment
    private MyViewPagerAdapter myFragmentAdapter;// 适配器
    private ViewPager mViewPager;
    private RadioButton buttonOne;
    private RadioButton buttonTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trend);
        settingTheStatebarAndNavigationbar();
        init();

    }

    /**
     * 设置沉浸式状态栏和底部虚拟键全屏
     */
    private void settingTheStatebarAndNavigationbar() {
        // 透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //底部虚拟键透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }


    /**
     * 获取界面对象并监听
     */
    private void init()
    {
        fragmentList = new ArrayList<Fragment>();
        weekFragment = TrendWeekFragment.newInstance("","");
        monthFragment = TrendMonthFragment.newInstance("","");
        fragmentList.add(weekFragment);
        fragmentList.add(monthFragment);
        myFragmentAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        // toolbar
        Toolbar toolbar = (Toolbar)findViewById(R.id.trend_toolbar);
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(myMenuListener);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrendActivity.this.onBackPressed();
            }
        });
        // 设置Radiobutton默认选中第一个
        buttonOne = (RadioButton)findViewById(R.id.button_week);
        buttonOne.setChecked(true);
        buttonTwo = (RadioButton)findViewById(R.id.button_month);
        // 获取RadioGroup对象，并监听选中项
        RadioGroup buttonGroup = (RadioGroup)findViewById(R.id.button_group);
        buttonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch (checkedId)
                {
                    case R.id.button_week:
                        // 点击周，显示fragment1
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.button_month:
                        // 点击月，显示fragment2
                        mViewPager.setCurrentItem(1);
                        break;
                }


            }
        });


        // 获取viewpager并设置适配器
        mViewPager = (ViewPager)findViewById(R.id.trend_viewpager);
        mViewPager.setAdapter(myFragmentAdapter);
        // 设置viewpager页面切换监听
        mViewPager.addOnPageChangeListener(new myPagerListener());




    }


    class myPagerListener implements ViewPager.OnPageChangeListener
    {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if(position == 0)
            {
                if(!buttonOne.isChecked())
                {
                    buttonOne.setChecked(true);
                }
            }
            else if(position == 1)
            {
                if(!buttonTwo.isChecked())
                {
                    buttonTwo.setChecked(true);
                }
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


    // 菜单监听器
    private Toolbar.OnMenuItemClickListener myMenuListener = new Toolbar.OnMenuItemClickListener()
    {
        @Override
        public boolean onMenuItemClick(MenuItem item)
        {
            switch (item.getItemId())
            {
                case R.id.trend_share_menu:
                    Intent intent = new Intent();
                    intent.setClass(TrendActivity.this, ShareActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    break;
            }
            return false;
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_share_simpel, menu);
        return true;
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
